<%--
  Copyright � 2005 Obeo.
  All rights reserved.   This program and the accompanying materials
  are made available under the terms of the GNU Public License v2.0 or any later version
  which accompanies this distribution, and is available at
  http://www.gnu.org/licenses/gpl.txt
--%>
<%
metamodel http://www.eclipse.org/uml2/2.0.0/UML

import org.acceleo.modules.uml2.services.Uml2Services
import org.acceleo.modules.uml2.services.StringServices
%>

<%-- 
This template deals with Struts actions
--%>

<%script type="Class" name="fullFilePath"%>
<%-- Only Classes with stereotype Screen--%>
<%if (hasStereotype("Screen")){%>
/<%scenarioPackage.toPath()%>/<%name%>Servlet.java
<%}%>

<%script type="Class" name="generate" file="<%fullFilePath%>"%>
<%-- Generate Struts actions--%>
package <%scenarioPackage%>;

// <%startUserCode%> for import
import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
// <%endUserCode%> for import

/**
 * Implementation of <strong><%name%> Servlet</strong>
 * Scenario : <%package.name%>
 *
 */
public class <%name%>Servlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	
    /**
     * Commons Logging instance.
     */
	private static final Log log = LogFactory.getLog(<%name%>Servlet.class);

    /**
     * Forward codes.
     */
	public final static String PAGE_SELF = "<%package.name%>/<%name%>.jsp"; //$NON-NLS-1$
<%-- class association navigation --%>
<%for (attribute){%><%if (type != null && type.hasStereotype("Screen")){%>
	public final static String PAGE_<%type.name.toUpperCase()%> = "<%type.package.name%>/<%name%>.jsp"; //$NON-NLS-1$
<%}%><%}%>

<%-- init action method generation (Dispach)--%>
    /**
     * Process the specified HTTP request for event.
     * @param requ The HTTP request we are processing
     * @param resp The HTTP response we are creating
     *
     * @exception Exception if an input/output error or servlet exception occurs
     */
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doAction(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doAction(req, resp);
	}
	
	protected void doAction(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String event = (String)req.getAttribute("event");
		log.debug("Find event : " + event);
		// <%startUserCode%> before calling specific action
		// TOFILL Write here the code to execute before any action of <%name%>Servlet
		// <%endUserCode%>
		
		if ( event == null ) {
			init(req, resp);
		}
		<%for (ownedOperation){%>
		if ("<%name%>".equals(event)) {
			this.<%name%>(req, resp);
		}
		<%}%>
	}
	
    /**
     * Process the specified HTTP request for the initial call to this servlet.
     * @param req The HTTP request we are processing
     * @param resp The HTTP response we are creating
     *
     * @exception Exception if an input/output error or servlet exception occurs
     */
	public void init(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		log.debug("Starting <%name%>.init");
		<%for (attribute){%>
			<%if (association == null){%>
		String <%name%> = (String)req.getAttribute("<%name%>");
			<%}%>
		<%}%>
		String returnCode = PAGE_SELF;
		//<%startUserCode%> init
		// TODO Write here the action code for <%name%>
		//<%endUserCode%>		
		RequestDispatcher dispatcher = req.getRequestDispatcher("/jsp/" + returnCode);
		if (dispatcher != null) {
			dispatcher.forward(req, resp);
		} else {
			throw new ServletException("Redirection failed because " + returnCode + " hasn't been found");
		}
	}

<%-- actions methods generation --%>
<%for (ownedOperation){%>
    /**
     * Process the specified HTTP request for <strong><%name%></strong> event.
     * @param req The HTTP request we are processing
     * @param resp The HTTP response we are creating
     *
     * @exception Exception if an input/output error or servlet exception occurs
     */
	public void <%name%>(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		log.debug("Starting <%class.name%>.<%name%>");
		<%for (class.attribute){%>
			<%if (association == null){%>
		String <%name%> = (String)req.getAttribute("<%name%>");
			<%}%>
		<%}%>
		//<%startUserCode%> method <%name%>
		String returnCode = PAGE_SELF;
		// TODO Write here the action code for <%name%>
		//<%endUserCode%>		
		RequestDispatcher dispatcher = req.getRequestDispatcher("/jsp/" + returnCode);
		if (dispatcher != null) {
			dispatcher.forward(req, resp);
		} else {
			throw new ServletException("Redirection failed because " + returnCode + " hasn't been found");
		}
	}
<%}%>

	// <%startUserCode%> user methods
	// <%endUserCode%>

}

<%script type="Class" name="scenarioPackage"%>
<%-- ui struts package --%>
org.acceleo.sample.ihm

